// 字典页面
import {DictSonFormType, DictSonTableType} from "@/api/sys/admin/dict-son/types";
import {Ref} from "vue";
import {deleteDictSonApi, getDictSonByIdApi, getDictSonsByDictIdApi} from "@/api/sys/admin/dict-son";
import {DictTableType} from "@/api/sys/admin/dict/types";
import {initPageQuery} from "@/utils/common-fn";
import {openConfirm} from "@/components/MyComponent/XMessageBox";
import {DictOptions} from "@/utils/common-types";
import {commonGetOptions} from "@/utils/format-table";

export const useDictSonViewStore = defineStore('dictSonView', () => {

    /**
     * options相关
     */
    const statusOptions: Ref<DictOptions[]> = ref([])
    /**
     * table相关
     */
    const title = '子字典'
    const initForm: DictSonFormType = {code: 1, status: 1, sort: 1}
    // table数据
    let tableData: Ref<DictSonTableType[]> = ref([])
    // 总数
    const total = ref(0)
    // 分页信息,以及查询条件,用于发送api
    const queryData = reactive(initPageQuery<DictSonTableType>({order: {sort: 'asc'}}))
    /**
     * 编辑相关
     */
        // 弹出框:编辑
    const editVisible = ref(false)

    // 编辑的表单数据
    const editForm: Ref<DictSonFormType> = ref(initForm)


    /**
     * 添加相关
     */
        // 弹出框:编辑
    const addVisible = ref(false)

    // 添加的表单数据
    const addForm: Ref<DictSonFormType> = ref(initForm)

    /**
     * 子字典可见相关
     */
    const sonDictVisible = ref(false)
    // 当前的父元素
    const currentDict: Ref<DictTableType> = ref({
        id: 0,
        name: '',
        code: '',
        status: 1,
        sort: 1,
    })

    /**
     * 方法相关
     */
    // 设置编辑对话框可见
    function setEditVisible(bolValue: boolean) {
        editVisible.value = bolValue
    }

    // 设置编辑对话框可见
    function setAddVisible(bolValue: boolean) {
        addVisible.value = bolValue
    }

    /**
     * 获取列表
     */
    async function getList() {
        [[tableData.value, total.value], statusOptions.value] = await Promise.all(
            [getDictSonsByDictIdApi(currentDict.value.id, queryData),
                ...commonGetOptions('status')]);
    }


    // 格式化状态
    function formatStatus(row: any, column: any, value: number) {
        if (value === 1) {
            return '正常'
        } else if (value === 0) {
            return '禁用'
        }
        return '异常'
    }


    // 打开 编辑
    async function openEdit(id: number) {
        // 编辑的时候,获取所有的角色
        editForm.value = await getDictSonByIdApi(id)
        editVisible.value = true
    }

    // 删除按钮
    async function openDelete(id: number) {
        if (await openConfirm('确定删除' + title)) {
            await deleteDictSonApi(id)
            ElMessage.success('删除成功')
            await getList()
        }
    }

    // 打开 添加
    async function openAdd() {
        addForm.value = {
            name: '',
            code: 1,
            // 1-正常 0-禁用
            status: 1,
            sort: 1,
            remark: '',
        }
        addVisible.value = true
    }

    // 再次点击就关闭
    let beforeId = 0

    // 点击父打开子
    async function showDictSon(column: DictTableType) {
        // 关闭
        if (beforeId === column.id && sonDictVisible.value) {
            sonDictVisible.value = false
        } else {
            // 打开
            // 获取值列表
            currentDict.value = column
            await getList()
            sonDictVisible.value = true
        }
        beforeId = column.id
    }

    return {
        tableData, queryData, total, editVisible, editForm, addForm, addVisible, sonDictVisible, currentDict,
        title, statusOptions,
        formatStatus, openEdit, setEditVisible, getList, openDelete,
        setAddVisible, openAdd, showDictSon
    }
})